package com.dao.impl;

import com.dao.IRevertDAO;
import com.vo.Revert;
import com.db.ConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Repository
public class RevertDAOImpl implements IRevertDAO {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Revert> getRevertsByMessage(int messageID) {
        String sql = "SELECT * FROM revert WHERE messageID = ? ORDER BY writeDate";
        return jdbcTemplate.query(sql, (rs, rowNum) -> {
            Revert revert = new Revert();
            revert.setRevertID(rs.getInt("revertID"));
            revert.setMessageID(rs.getInt("messageID"));
            revert.setContent(rs.getString("content"));
            revert.setWriter(rs.getString("writer"));
            revert.setWriteDate(rs.getTimestamp("writeDate"));
            return revert;
        }, messageID);
    }

    @Override
    public void addRevert(Revert revert) {
        String sql = "INSERT INTO revert (messageID, content, writer, writeDate) VALUES (?, ?, ?, ?)";
        jdbcTemplate.update(sql,
                revert.getMessageID(),
                revert.getContent(),
                revert.getWriter(),
                new Timestamp(revert.getWriteDate().getTime()));

        System.out.println("已添加回复到留言ID：" + revert.getMessageID());
    }

    @Override
    public void deleteRevert(int revertID) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = ConnectionManager.getConnection();
            String sql = "DELETE FROM revert WHERE revertID=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, revertID);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeAll(conn, pstmt, null);
        }
    }

    @Override
    public void deleteRevertsByMessage(int messageID) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = ConnectionManager.getConnection();
            String sql = "DELETE FROM revert WHERE messageID=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, messageID);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeAll(conn, pstmt, null);
        }
    }

    @Override
    public Revert getRevertById(int revertID) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Revert revert = null;

        try {
            conn = ConnectionManager.getConnection();
            String sql = "SELECT * FROM revert WHERE revertID=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, revertID);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                revert = new Revert();
                revert.setRevertID(rs.getInt("revertID"));
                revert.setMessageID(rs.getInt("messageID"));
                revert.setContent(rs.getString("content"));
                revert.setWriter(rs.getString("writer"));
                revert.setWriteDate(rs.getTimestamp("writeDate"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeAll(conn, pstmt, rs);
        }
        return revert;
    }
}